package fileWorker;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import javax.naming.AuthenticationException;

import adProvider.ADConnection;
import adProvider.ADQuery;

public class SetMail {

	public void setMail() {

		ADConnection adc = new ADConnection();

		adc.useSSL(true);

		adc.setSERVER("S04");
		adc.setROOT("moniki.local");

		adc.setLOGIN("task");
		adc.setPASS("435ZfpCQ");

		try {
			adc.connect();
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		ADQuery adq = new ADQuery(adc);

		String filter = "(&(objectCategory=user)(!(mail=*)))";
		ArrayList<String> list = adq.getListDN(filter);

		Iterator<String> i = list.iterator();
		while (i.hasNext()) {
			String dn = i.next();
			System.out.println(dn);

			Hashtable<String, Object> attrs;
			attrs = adq.getAttributeByNames(dn, "userPrincipalName");

			String mail = (String) attrs.get("userPrincipalName");

			if (mail != null) {
				attrs.clear();

				attrs.put("mail", mail);
				adq.setAttributes(dn, attrs);

			}

			// else {
			// continue;
			// }
			// break;
		}

		adc.close();
	}

	public static void main(String[] args) {
		SetMail ms = new SetMail();
		ms.setMail();
	}

}
